﻿#region

using System.Collections.Generic;

#endregion

namespace Domain.Algorithms {
    public static class ToMAcyclicGraphCompleter {
        public static void Complete(ref Graph<HyperEdge> graph, ref List<HyperEdge> newEdges) {
            var vertexedGraph = SupportGraphsBuilder.FindVertexedGraph(graph);

            if (!TriangulatedGraphUtils.IsGraphTriangulated(vertexedGraph)) {
                /*Situation 2,3 in the article*/
                TriangulatedGraphUtils.TriangulateGraph(ref vertexedGraph);
            }
            else {
                /*Situation 1 in the article*/
            }

            var allMaximumCliques = TriangulatedGraphUtils.FindAllMaximumCliques(vertexedGraph);

            foreach (var clique in allMaximumCliques) {
                var needToAddClique = true;
                foreach (var edge in graph.Edges) {
                    if (!GraphUtils<HyperEdge>.IsIncluded(clique, edge.Vertices)) {
                        continue;
                    }
                    needToAddClique = false;
                    break;
                }
                if (needToAddClique) {
                    var hyperEdge = new HyperEdge(clique.ToArray());
                    graph.AddEdge(hyperEdge);
                    newEdges.Add(hyperEdge);
                }
            }
        }
    }
}